<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\StaffLogic;
use Illuminate\Http\Request;

/**
 * Class StaffController
 * @package App\Http\Controllers\OA
 * @desc 员工相关逻辑
 */
class StaffController extends Controller
{

    public function getStaffList(Request $request)
    {
        $rules = [
            'username'=>'present|string|nullable',
            'state'=>'present|numeric|nullable',
            'account'=>'present|string|nullable',
            //'pageSize'=>'required|numeric|min:10',
            //'page'=>'required|numeric|min:1',
        ];
        $this->verify($request->all(),$rules);

        try{
            $logic = new StaffLogic();
            $list = $logic->getStaffList($request);
             ret_json('ok','ok',$list);
        }catch(\Exception $e){
             ret_json('err',$e->getMessage());
        }
    }

    //批量删除
    public function BatchDelete(Request $request){
        $rules = [
            'id_list'=>"required|array",
        ];
        $this->verify($request->all(),$rules);
        try{
            $logic = new StaffLogic();
            $result = $logic->BatchDelete($request->all());
            if($result) ret_json('ok','删除成功');
            ret_json('err','删除失败');
        }catch(\Exception $e){
            ret_json($e->getMessage());
        }
    }

    //获取用户详情
    public function StaffInfo(Request $request)
    {
        $rules = ['id'=>'required|numeric'];
        $this->verify($request->all(),$rules);
        try{
            $logic = new StaffLogic();
            $info = $logic->getStaffInfo($request->all());
            ret_json('ok','ok',$info);
        }catch(\Exception $e){
            dd($e);
            ret_json('err',$e->getMessage());
        }
    }

    //编辑员工
    public function StaffEdit(Request $request)
    {
        $rules = [
            'account'=>'present|string',
            'username'=>'required|string',
            'phone'=>'present|nullable|string',
            'email'=>'present|nullable|string',
            'sex'=>'required|numeric|between:0,2',
            'status'=>'required|numeric|between:0,2',
            'roleid'=>'required|array',
            'deptid'=>'required|array',
            'id'=>'required:exists:staff',
            'id_card'=>'present|nullable|string',
            'avatar'=>'present|nullable|string',
            'address'=>'present|nullable|string',
        ];

        $this->verify($request->all(),$rules);
        //dd($request->all());
        try{
            $logic = new StaffLogic();
            $result = $logic->staffEdit($request->all());
            if(!$result) ret_json('err','更新失败');
            ret_json('ok','更新成功');
        }catch(\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取员工职位,角色
    public function StaffPositionRole(Request $request){
        $rules = ['id'=>'required|exists:staff,id'];
        $this->verify($request->all(),$rules);
        try{
         $logic = new StaffLogic();
          $user_info = $logic->StaffPositionRole($request->all());
            ret_json('ok','请求成功',$user_info);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }



}
